﻿#include "mouseacts.h"
MouseActs::MouseActs(QObject *parent)
    : QObject (parent)
{
}

//执行鼠标动作
void MouseActs::doPressAct(int x,int y,int x1,int y1,int type_id)
{
    SetCursorPos(x,y);//必须要先设置一下鼠标的初始位置，不然会导致不成功

    //获取屏幕尺寸大小
    QDesktopWidget * desktop = QApplication::desktop();
    QRect screen_rect = desktop->screenGeometry(0);
    int desktop_width = screen_rect.width();
    int desktop_height = screen_rect.height();
    qDebug()<<"desktop_width"<<desktop_width;
    qDebug()<<"desktop_height"<<desktop_height;

    if(type_id==1)
    {
        //鼠标左键单击
        mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_LEFTDOWN,x, y, 0, 0);//按下
        mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_LEFTUP,x, y, 0, 0);//松开
    }
    else if(type_id==2)
    {
        //鼠标左键双击
        mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_LEFTDOWN,x, y, 0, 0);
        mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_LEFTUP,x, y, 0, 0);
        mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_LEFTDOWN,x, y, 0, 0);
        mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_LEFTUP,x, y, 0, 0);

    }else if(type_id==3)
    {
        //鼠标右键单击
        mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_RIGHTDOWN,x, y, 0, 0);
        mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_RIGHTUP,x, y, 0, 0);
    }else if(type_id==4)
    {
        //鼠标右键双击
        mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_RIGHTDOWN,x, y, 0, 0);
        mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_RIGHTUP,x, y, 0, 0);
        mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_RIGHTDOWN,x, y, 0, 0);
        mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_RIGHTUP,x, y, 0, 0);
    }else if(type_id==5)
    {
        //鼠标移动
        mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_MOVE ,
                    x * 65536 / desktop_width,y * 65536 / desktop_height,0,GetMessageExtraInfo());

    }else if(type_id==6)
    {
        //鼠标拖拽
        mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_LEFTDOWN,x, y, 0, 0);
        mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_MOVE ,
                    x1 * 65536 / desktop_width,y1 * 65536 / desktop_height,0,GetMessageExtraInfo());
        mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_LEFTUP,x1, y1, 0, 0);

        qDebug()<<"--"<<x<<","<<y<<"to"<<x1<<","<<y1;
    }
}
